﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class CVNew : ICPRelationshipPage
{
    public CVData cvData = new CVData();

    #region Properties
    
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Customer Visit - Create New";
        if (!IsPostBack && !IsCallback)
        {
        }
    }
    protected void cbkCVNew_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = CreateNewCV(e.Parameter);
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    private string CreateNewCV(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxReturn = String.Empty;

        foreach (String sxRow in sxSplitted)
        {
            String[] sxRowSplitted = sxRow.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

            if (sxRowSplitted[0].ToUpper().Contains(KCV.CP_ID))
            {
                String[] sxIDSplitted = sxRowSplitted[1].Split('_');
                bool IsNewCP = sxIDSplitted[1].ToUpper() == KGlobal.YES;

                int iCPId = Convert.ToInt32(sxIDSplitted[0]);
                if (IsNewCP)
                {
                    cvData.iNewContactPersonID = iCPId;
                }
                else
                {
                    cvData.iContactPersonID = iCPId;
                }
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KCV.VISIT_DATETIME))
            {
                cvData.dtmVisitDatetime = Convert.ToDateTime(sxRowSplitted[1]);
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KCV.VISIT_STATUS))
            {
                cvData.sxVisitStatus = sxRowSplitted[1].ToString();
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KCV.EXPECTATION))
            {
                cvData.iP_Customer_Expectation_ID = Convert.ToInt32(sxRowSplitted[1]);
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KCV.PURPOSE))
            {
                cvData.iP_Visit_Purpose_ID = Convert.ToInt32(sxRowSplitted[1]);
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KCV.CUSTOMER_ID))
            {
                cvData.iCustomerID = Convert.ToInt32(sxRowSplitted[1]);
            }
        }

        DBQuery.CustomerVisitTable.SaveVisit(cvData);
        sxReturn = cvData.iCVID.ToString();

        if (cvData.sxVisitStatus.ToUpper() == "PLANNED")
        {
            DBQuery.NotificationTable.InsertNotification_UserEvent(sxReturn, ICPUser.ID, cvData.iCustomerID,
                UserEventType.CustomerVisit, cvData.dtmVisitDatetime);
        }
        else if (cvData.sxVisitStatus.ToUpper() == "DIRECT")
        {
            Cached.CustomerVisitTable cachedCV = new Cached.CustomerVisitTable();
            DataTable dtVisitData = cachedCV.GetVisitDetailById(cvData.iCVID);
            CVData cvData2 = new CVData();
            if (dtVisitData.Rows.Count > 0)
            {
                cvData2.iCVID = cvData.iCVID;
                cvData2.dtmVisitDatetime = Convert.ToDateTime(dtVisitData.Rows[0]["VISIT_DATETIME"]);
                cvData2.sxVisitStatus = dtVisitData.Rows[0]["VISIT_STATUS"].ToString();
                cvData2.sxCreateBy = dtVisitData.Rows[0]["CREATE_BY_NAME"].ToString();
                if (dtVisitData.Rows[0]["P_CUSTOMER_EXPECTATION_ID"] != DBNull.Value)
                {
                    cvData2.iP_Customer_Expectation_ID = Convert.ToInt32(dtVisitData.Rows[0]["P_CUSTOMER_EXPECTATION_ID"]);
                }
                cvData2.iP_Visit_Purpose_ID = Convert.ToInt32(dtVisitData.Rows[0]["P_VISIT_PURPOSE_ID"]);
                cvData2.sxVisitPurpose = dtVisitData.Rows[0]["VISIT_PURPOSE"].ToString();
                if (dtVisitData.Rows[0]["CUSTOMER_EXPECTATION"] != DBNull.Value)
                {
                    cvData2.sxCustomerExpectation = dtVisitData.Rows[0]["CUSTOMER_EXPECTATION"].ToString();
                }
                cvData2.sxCompanyName = dtVisitData.Rows[0]["COMPANY_NAME"].ToString();
                cvData2.sxContactPersonName = dtVisitData.Rows[0]["NAME"].ToString();
                cvData2.iCustomerID = Convert.ToInt32(dtVisitData.Rows[0]["CUSTOMER_ID"]);
            }
            //SendMailExample(cvData2, ICPUser.ID);
        }
        return sxReturn;
    }
    public void SendMailExample(CVData cvData, String sxCARId)
    {
        if (!String.IsNullOrEmpty(sxCARId))
        {
            DataTable dtAssManEmail = DBQuery.EventTable.GetAssManForEmail(Convert.ToInt32(sxCARId));
            foreach (DataRow drAssManEmail in dtAssManEmail.Rows)
            {
                Mailer mail = new Mailer();
                mail.EmailSender = new System.Net.Mail.MailAddress("donot-reply@icp.lintasarta.co.id", "ICP Bots");
                mail.EmailSubject = "You have been notified";
                //mail.AddRecipient("irfan.hilmy@lintasarta.co.id");
                mail.AddRecipient(drAssManEmail["EMAIL"].ToString());
                mail.AddBCC("bayuardi.rahmanto@lintasarta.co.id");
                mail.EmailHTMLBody = CVManager.Build_HTMLMailBody(cvData);
                mail.EmailPlainText = CVManager.Build_PlainMailBody(cvData);
                //mail.SendMail();
            }
        }
    }
    #endregion Custom Methods
}
